package cn.edu.njuit.children.apis;

import cn.edu.njuit.children.common.Result;
import cn.edu.njuit.children.entity.Children;
import cn.edu.njuit.children.entity.Height;
import cn.edu.njuit.children.entity.UserAccount;
import cn.edu.njuit.children.entity.model.HeightModel;
import cn.edu.njuit.children.mapper.ChildrenMapper;
import cn.edu.njuit.children.mapper.HeightMapper;
import cn.edu.njuit.children.service.ChildrenService;
import cn.edu.njuit.children.service.HeightService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Description: TODO
 * @Data: 2022/11/29 15:07
 * @Author: guzy
 */
@RestController
@RequestMapping("/api/height")
@Api(value = "身高记录接口", tags = "身高记录接口")
public class HeightApi {
    @Autowired
    HeightService service;
    @Autowired
    HeightMapper mapper;

    @Autowired
    ChildrenService childrenService;

    @PostMapping
    public Result save(@Valid HeightModel o) throws Exception {
        boolean b = service.addHeight(o);
        if (b){
            return Result.success("添加成功");
        }else {
            return Result.failure("添加失败");
        }
    }

    @PutMapping
    public Result update(@Valid HeightModel o) throws Exception {
        boolean b = service.upHeight(o);
        if (b){
            return Result.success("修改成功");
        }else {
            return Result.failure("修改失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(service.removeById(id));
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(service.getById(id));
    }

    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(
            @RequestParam(defaultValue = "0",required = false) Integer currentPage,
            @RequestParam(defaultValue = "10",required = false) Integer pageSize,
            @RequestParam(required = false) Long childrenId) {
        Children children = childrenService.getById(childrenId);
        if (children==null){
            return Result.failure("未找到幼儿信息");
        }
        LambdaQueryWrapper<Height> wrapper = new LambdaQueryWrapper<Height>();
            wrapper.eq(Height::getCdId,childrenId);
        IPage<Height> page = new Page<Height>(currentPage, pageSize);
        mapper.selectPage(page, wrapper);
        return Result.success(page);
    }
}
